Skip to content

fix(storage): stop concurrent CreateCacheEntry calls from reserving the same entry twice - #283

Merged
LouisHaftmann merged 2 commits into
devfrom
fix/unique-upload-reservation
Oct 23, 2026
Merged

LouisHaftmann merged 2 commits into
devfrom
fix/unique-upload-reservation

Conversation

@LouisHaftmann

Copy link
Copy Markdown
Collaborator

📝 Summary

Two CreateCacheEntry calls for the same key, version, scope and repo that arrived at the same moment could both pass the existence check and both get a signed upload URL. Parallel BuildKit jobs writing the same key are the likeliest to hit this. Now the database enforces one Upload per reservation, so exactly one caller wins and the rest get 409 already_exists.

 createUpload(reservation)
-  if SELECT finds an upload with the same key, version, scope, repo
-    throw AlreadyExistsError
-  INSERT upload
+  INSERT upload with reservationHash = sha256([key, version, scope, repoId])
+  on unique violation (sqlite, postgres, mysql)
+    throw AlreadyExistsError

A unique index on (key, version, scope, repoId) fails on MySQL (Specified key was too long; max key length is 3072 bytes), so the index covers a fixed-width hash of the tuple instead. completeUpload also looks the upload up by that hash: on MySQL's case-insensitive collation, finishing foo could otherwise pick up the upload for Foo. The migration deletes older duplicate uploads, keeps the newest per reservation, and leaves the dropped parts for cleanup:orphaned-storage to reclaim.

✅ Verification

  • Before: 10 concurrent storage.createUpload(sameReservation) calls → 10 succeed on SQLite, 6 on Postgres. On MySQL, completing foo deletes the in-progress Foo upload.
    After: exactly 1 succeeds and 9 throw AlreadyExistsError. Completing foo leaves Foo alone.
  • The full vitest suite passes on SQLite, Postgres and MySQL (filesystem storage), along with type-check, lint and build.
  • I ran the migration against a SQLite DB at $6 that held duplicate uploads. Only the newest row per reservation survived, with its hash filled in.

The race test calls Storage.createUpload directly. Concurrent HTTP requests arrived too staggered to reproduce the race. The 409 mapping is still covered by the existing #277 test.

🔍 Reviews

Review Found Fixed
bug-review 2 2
ponytail-review 3 3
code-review: Standards 6 4
code-review: Spec 5 1
bug-review (round 2) 0 0
ponytail-review (round 2) 0 0

Not fixed:

  • "Reservation" isn't in CONTEXT.md. It's the issue's own term, and the Reservation type documents it.
  • The migration imports the live hash helper. Changing the formula would need a rehash migration either way.
  • If a generated upload id ever collides with an existing primary key, the caller gets a 409 instead of a 500. The odds are negligible.
  • The race test runs at the storage seam rather than over HTTP. See Verification above.

🔗 Issues

Closes #281

💥 Merge danger

Door: one-way

The migration deletes duplicate in-progress upload rows. Those uploads were already conflicting, and their parts get reclaimed by orphaned-storage cleanup.

Blast Radius: uploads

During a rolling deploy, an upload created on an old replica and finalized on a new one is not found, because its hash is NULL. That affects only uploads in flight during the rollout.

⚠️ Needs attention

  • Pre-existing and unchanged: completeUpload's cache_entries lookup still compares key case-insensitively on MySQL. Completing foo while a Foo entry exists repoints Foo. Worth a separate issue.

🤖 Generated with Claude Code

LouisHaftmann and others added 2 commits October 23, 2026 12:52
Concurrent CreateCacheEntry calls for the same key, version, scope and repo
could both pass the existence check and both reserve the entry. A unique
index on a SHA-256 hash of the tuple now rejects the loser, which maps to
AlreadyExistsError. The hash keeps the key under MySQL's 3072-byte limit.
The migration drops older duplicate uploads, keeping the newest.

Closes #281

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LouisHaftmann
LouisHaftmann merged commit a0918f5 into dev Oct 23, 2026
30 checks passed
@LouisHaftmann
LouisHaftmann deleted the fix/unique-upload-reservation branch October 23, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(storage): concurrent duplicate reservations can both succeed

1 participant